package com.norbitltd.guavaleaves.reflection;

import java.lang.reflect.Method;

import org.apache.commons.lang3.StringUtils;

import com.google.common.base.Predicate;

public class ReflectionPredicates {

	public static <T extends Object> Predicate<T> isPropertyTrue(final String propertyName) {
		final String methodName = "is" + StringUtils.capitalize(propertyName);
		return isMethodTrue(methodName);
	}
	
	public static <T extends Object> Predicate<T> isMethodTrue(final String methodName) {
		return new Predicate<T>() {
			public boolean apply(T input) {
				try {
					Method method = input.getClass().getMethod(methodName);	
					Object result = method.invoke(input);
					return Boolean.parseBoolean(result.toString());
				} catch(Exception e) {
					throw new RuntimeException("Unable to get or execute " + methodName + " method for an object!");
				}
			}
		};
	}
	
	public static <T extends Object> Predicate<T> isPropertyFalse(final String propertyName) {
		final String methodName = "is" + StringUtils.capitalize(propertyName);
		return isMethodFalse(methodName);
	}
	
	public static <T extends Object> Predicate<T> isMethodFalse(final String methodName) {
		return new Predicate<T>() {
			public boolean apply(T input) {
				try {
					Method method = input.getClass().getMethod(methodName);	
					Object result = method.invoke(input);
					return !Boolean.parseBoolean(result.toString());
				} catch(Exception e) {
					throw new RuntimeException("Unable to get or execute " + methodName + " method for an object!");
				}
			}
		};
	}
	
	
}
